Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work, you got the methods you wrote working well. However you need to see my notes on time complexity. Remember each recursive call puts things on the system stack.
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def fib(n) |
There was a problem hiding this comment.
This works, but see my video on time and space complexity.
| end | ||
| end | ||
|
|
||
| # Time complexity: O(n), where n isnthe length of the input |
There was a problem hiding this comment.
This is actually O(n^2) because s[0...-1] creates a new string and you do it n times.
| if s.nil? || s.length == 0 || s.length == 1 | ||
| return s | ||
| else | ||
| return s[-1] + reverse(s[0...-1]) |
There was a problem hiding this comment.
This is not in place since you create a new string.
Think about making a helper which takes the string, a left index and right index. The base case is when left_index >= right_index Each recursive call you swap the characters at left_index or right_index and then make a recursive call with the string, and left_index + 1 and right_index -1.
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) because additional space in memory is not being consumed |
There was a problem hiding this comment.
O(n) since you use the system stack for the recursion.
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the size of the input | ||
| # Space complexity: O(1) because additional space in memory is not being consumed |
There was a problem hiding this comment.
O(n^2) for both time and space since you use the system stack and create a new string with each iteration.
No description provided.